package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zk/api/result"
	"zk/internal/dao"
	"zk/internal/model"
)

func UserInteractionAdd(c *gin.Context) {
	var req result.UserInterachtionReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": err.Error(),
		})
		return
	}
	user := dao.GetUserBy(req.Comment)
	if user.ID != 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "互动已存在",
			"date": nil,
		})
		return
	}
	userIn := model.UserInteraction{
		UserId:  req.UserId,
		Comment: req.Comment,
	}
	if !dao.CreateBy(&userIn) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "发送失败",
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "创建成功",
		"date": userIn,
	})
}

func UserInteractionList(c *gin.Context) {
	var req result.UserInterachtionReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": err.Error(),
		})
		return
	}

	userIn := model.UserInteraction{
		UserId:  req.UserId,
		Comment: req.Comment,
	}
	if !dao.CreateBy(&userIn) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "创建失败",
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "创建成功",
		"date": userIn,
	})
}

func UserInteractionUpdate(c *gin.Context) {
	var req result.UpdateUserInReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": err.Error(),
		})
		return
	}
	room := dao.GetUpdateUserIn(req.Id, &model.UserInteraction{})
	if room.ID == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "修改失败",
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"date": room,
	})
}

func UserInteractionDelete(c *gin.Context) {
	var req result.DeleteUserInReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"date": err.Error(),
		})
		return
	}
	userIn := dao.GetUserInById(req.Id)
	if userIn.ID != 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "直播间不存在",
			"date": nil,
		})
		return
	}
	if !dao.GetDeleteUserIn(model.UserInteraction{}) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "删除失败",
			"date": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "直播间删除成功",
		"date": userIn,
	})
}
